package com.dodoke.service.impl;

import com.dodoke.common.core.domain.model.LoginUser;
import com.dodoke.domain.WxLoginUser;
import com.dodoke.domain.WxUser;
import com.dodoke.service.IWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/***
 * 没有openId则注册，有则更新用户信息
 * 这一步只是创建登录用户这个对象而已
 * 返回的对象要附带上权限信息
 */
@Service("WxUserDetailServiceImpl")
public class WxUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private IWxUserService wxUserService;

    @Override
    public UserDetails loadUserByUsername(String openId) throws UsernameNotFoundException {
        WxUser wxUserResult = wxUserService.selectOpenId(openId);
        if (wxUserResult != null){
            // 最后都返回WxLoginUser,将当前对象包含进去
            return new WxLoginUser(wxUserResult);
        }
        return null;
    }
}
